package com.sky.cqculture.Contraller;

import com.github.pagehelper.PageInfo;
import com.sky.cqculture.pojo.Product;
import com.sky.cqculture.pojo.Result;
import com.sky.cqculture.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/products")
public class ProductController {
    
    @Autowired
    private ProductService productService;
    
    /**
     * 根据ID获取商品详情
     * GET /api/products/{id}
     */
    @GetMapping("/{id}")
    public Result getProductById(@PathVariable Integer id) {
        Product product = productService.findProductById(id);
        if (product == null) {
            return Result.error("商品不存在");
        }
        return Result.success(product);
    }
    
    /**
     * 根据分类ID获取商品列表
     * GET /api/products/category/{categoryId}
     */
    @GetMapping("/category/{categoryId}")
    public Result getProductsByCategory(@PathVariable Integer categoryId) {
        List<Product> products = productService.findProductsByCategoryId(categoryId);
        return Result.success(products);
    }
    
    /**
     * 获取爆款商品
     * GET /api/products/popular
     */
    @GetMapping("/popular")
    public Result getPopularProducts(@RequestParam(defaultValue = "8") Integer limit) {
        List<Product> products = productService.getPopularProducts(limit);
        return Result.success(products);
    }
    
    /**
     * 分页查询商品（推荐使用这个接口获取商品列表）
     * GET /api/products/page
     */
    @GetMapping("/page")
    public Result getProductsByPage(
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(defaultValue = "default") String sortType,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        
        PageInfo<Product> pageInfo = productService.findProductsByPage(categoryId, sortType, pageNum, pageSize);
        return Result.success(pageInfo);
    }
}